package com.foreknow.controller;

import com.foreknow.model.Admin;
import com.foreknow.service.impl.AdminServiceimpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class JsonLogin extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        AdminServiceimpl adminService=new AdminServiceimpl();
        String name = req.getParameter("name");
      Admin admin =adminService.getUserame(name);
               Gson gson = new Gson();//需要导入gson架包
               String json = gson.toJson(admin);
               System.out.println(json);


        if (admin!=null){
            json="{\"admin\":"+true+"}";
        }else {
            json="{\"admin\":"+false+"}";
        }
        out.println(json);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
